package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author Just、倾城
 * @created 2025-10-15
 * @description
 *
 */
@RestController
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping("/pool")
    public Result listPool(BusinessPoolDTO dto){
        PageResult<BusinessDO> data = businessService.listPool(dto);
        return Result.success(data);
    }

    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id){
        businessService.deleteById(id);
        return Result.success();
    }
    /*
    * 商机转客户
    * @Param
    * @return
    *
    * */
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        businessService.toCustomer(id);
        return Result.success();
    }
    /*
    * 跟进商机
    * @Param
    * @return
    *
    * */

    @PutMapping
    public Result updateBusinessById(@RequestBody UpdateBusinessInfoDTO updateBusinessInfoDTO){
        businessService.updateBusinessInfoById(updateBusinessInfoDTO);
        return Result.success();
    }

    /**
     * 根据id查询 商机信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id){
        BusinessVO vo = businessService.getBusinessById(id);
        return Result.success(vo);
    }

    /**
     * 商机分配
     * @param businessId
     * @param userId
     * @return
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateBusinessById(@PathVariable Integer businessId,@PathVariable Integer userId){
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }
    /**
     * 添加业务
     * @param businessDTO
     * @return
     */
    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO businessDTO) {
        businessService.saveBusiness(businessDTO);
        return Result.success();
    }

    /**
     * 商机列表查询
     * @param
     * @return
     */
    @GetMapping
    public Result listBusiness(BusinessListDTO businessListDTO) {
        PageResult<BusinessDO> businessDO = businessService.businessList(businessListDTO);
        return Result.success(businessDO);
    }
}
